package com.cangsan.erhai.config;

import com.cangsan.erhai.security.*;
import com.cangsan.erhai.service.SysUserService;
import com.cangsan.erhai.service.impl.MyUserServiceDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SeucrityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private LoginSuccessHandler loginSuccessHandler;
    @Autowired
    private JwtLogoutSuccessHandler jwtLogoutSuccessHandler;

    @Autowired
    private LoginFailureHandler loginFailureHandler;
    @Autowired
    private MyUserServiceDetailsImpl userServiceDetails;
    @Autowired
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Autowired
    private WechatAuthSuccessHandler wechatAuthSuccessHandler;

    @Autowired
    private WechatAuthFailureHandler wechatAuthFailureHandler;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private WechatConfig wechatConfig;




    private static final String URL_WHITELIST[] ={
            "/login",
            "/logout",
            "/captcha",
            "/password",
            "/image/**",
            "/files/**",
            "/user/**",
            "/dict/**",
            "/carousel/**",
            "/contest/**",
            "/course/**",
            "/football/**",
            "/seek/**",
            "/video/**",
            "/dict/**",
            "/menu/**",
            "/role/**",
            "/order/**",
            "/vip/**",
            "/seek/**",
            "/contestmake/**"
    } ;
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){

        return new BCryptPasswordEncoder();
    }



    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        JwtAuthenticationFilter jaf = new JwtAuthenticationFilter(authenticationManager());
        return jaf;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //开启跨域 csrf攻击关闭
        http.cors().and().csrf().disable();
        //登出登出配置
             http
                .formLogin()
                .successHandler(loginSuccessHandler)
                     .failureHandler(loginFailureHandler)
                     .and()
                     .logout()
                     .logoutSuccessHandler(jwtLogoutSuccessHandler);

        //session禁用
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //拦截规则
        http.authorizeRequests().antMatchers(URL_WHITELIST).permitAll()
                .anyRequest().authenticated();
        //自定义过滤配置
        http.addFilter(jwtAuthenticationFilter());
        http.addFilterBefore(weChatAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
        //自定义jwt异常
        http.exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint);
    }

    @Bean
    public WechatAuthenticationFilter weChatAuthenticationFilter() throws Exception {
        WechatAuthenticationFilter filter = new WechatAuthenticationFilter();
        filter.setAuthenticationManager(authenticationManagerBean());
        filter.setAuthenticationSuccessHandler(wechatAuthSuccessHandler);
        filter.setAuthenticationFailureHandler(wechatAuthFailureHandler);
        return filter;
    }
    @Bean
    public WeChatAuthenticationProvider weChatAuthenticationProvider() {
        return new WeChatAuthenticationProvider(wechatConfig,sysUserService);
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 添加微信登录认证提供者
        auth.authenticationProvider(weChatAuthenticationProvider());
        // 添加用户名密码登录认证提供者
        auth.authenticationProvider(daoAuthenticationProvider());
    }

    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(userServiceDetails);
        provider.setPasswordEncoder(new BCryptPasswordEncoder());
        return provider;
    }






}
